/*
 * @Author: szx
 * @Date: 2022-02-12 14:52:12
 * @LastEditTime: 2022-02-12 15:03:29
 * @Description:
 * @FilePath: \leetcode\1200-1299\1219\1219.js
 */
/**
 * @param {number[][]} grid
 * @return {number}
 */
var getMaximumGold = function (grid) {
    const dirs = [
        [-1, 0],
        [1, 0],
        [0, -1],
        [0, 1]
    ];
    const m = grid.length;
    const n = grid[0].length;
    const visited = new Array(m).fill(0).map(() => new Array(n).fill(false));
    const dfs = (row, col, num) => {
        if (row < 0 || row >= m || col < 0 || col >= n || grid[row][col] == 0 || visited[row][col]) return num;
        visited[row][col] = true;
        num += grid[row][col];
        let tmp = num;
        for (let dir of dirs) {
            const res = dfs(row + dir[0], col + dir[1], num);
            tmp = Math.max(res, tmp);
        }
        visited[row][col] = false;
        return tmp;
    };
    let res = 0;
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (grid[i][j] > 0) {
                res = Math.max(dfs(i, j, 0), res);
            }
        }
    }
    return res;
};
console.log(
    getMaximumGold( [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]])
);
